﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLayer;
using BusinessLayer.DTO;
using BahdlClassLib;

namespace PayRoll
{
    public partial class ManageAttendance : TransactionMasterForm
    {
        List<AttendanceInfo> lstAttendanceInfo = new List<AttendanceInfo>();
        public ManageAttendance()
        {
            InitializeComponent();
        }

        private void Attendance_Load(object sender, EventArgs e)
        {
            try
            {
                this.Left = 0;
                this.Top = 0;
                ddlDate.Value = DateTime.Now;
                ddlTime.Value = DateTime.Now;
                txtEmpName.Tag = 0;
                this.WindowState = FormWindowState.Maximized;
                ClsWorkingHours ClClsWorkingHours = new ClsWorkingHours();
                bool found = ClClsWorkingHours.GetCurrentWorkingHour(DateTime.Now, DateTime.Now);
                if (found)
                {
                    string fromDate = Utility.GetDateToDisplay(ClClsWorkingHours.DateFrom);
                    string toDate = Utility.GetDateToDisplay(ClClsWorkingHours.DateTo);
                    string fromTime = Utility.GetTimeToDisplay(ClClsWorkingHours.StartWorkingTime);
                    string toTime = Utility.GetTimeToDisplay(ClClsWorkingHours.OffWorkingTime);
                    string gracePeriod = ClClsWorkingHours.MinimumWorkingHour.ToString();
                    decimal noOfHours = ClClsWorkingHours.NoOfHours;
                    // string info = "From " + fromDate + " to " + toDate + " Working hour is from " + fromTime + " to " + toTime + " and Minimum working hour is " + gracePeriod + " hours";


                    richTextBoxWorkingHour.Text = "";
                    richTextBoxWorkingHour.SelectionColor = Color.Blue;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(" From ");

                    richTextBoxWorkingHour.SelectionColor = Color.Brown;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(fromDate);

                    richTextBoxWorkingHour.SelectionColor = Color.Blue;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(" to ");

                    richTextBoxWorkingHour.SelectionColor = Color.Brown;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(toDate);

                    richTextBoxWorkingHour.SelectionColor = Color.Blue;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(" working hour is from ");

                    richTextBoxWorkingHour.SelectionColor = Color.Brown;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(fromTime);

                    richTextBoxWorkingHour.SelectionColor = Color.Blue;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(" to ");

                    richTextBoxWorkingHour.SelectionColor = Color.Brown;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(toTime);

                    richTextBoxWorkingHour.SelectionColor = Color.Blue;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(" and minimum working hour is  ");

                    richTextBoxWorkingHour.SelectionColor = Color.Brown;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(gracePeriod);

                    richTextBoxWorkingHour.SelectionColor = Color.Blue;
                    richTextBoxWorkingHour.SelectionFont = new Font("Times New Roman", 9, FontStyle.Bold);
                    richTextBoxWorkingHour.AppendText(" hours.");


                }
                else
                {
                    MessageBox.Show("Working hour is not Configured. Please contact Administrator", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }


        private void btnSearch_Click(object sender, EventArgs e)
        {
            int employeeID = 0;
            int.TryParse(txtEmpName.Tag.ToString(), out employeeID);
            if (employeeID == 0)
            {
                MessageBox.Show("Plase select employee", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            lstAttendanceInfo = new List<AttendanceInfo>();
            ClsManageAttendance objClsManageAttendance = new ClsManageAttendance();
            DataTable dt = objClsManageAttendance.GetEmployeeAttendance(Convert.ToInt32(txtEmpName.Tag), Convert.ToDateTime(dtdFromDate.Text), Convert.ToDateTime(dtdToDate.Text));
            ClsWorkingHours clsWorkingHours = new ClsWorkingHours();
            foreach (DataRow dr in dt.Rows)
            {
                AttendanceInfo objAttendanceInfo = new AttendanceInfo();
                objAttendanceInfo.AttendanceID = Convert.ToInt32(dr["AttendanceID"]);
                objAttendanceInfo.EmployeeID = Convert.ToInt32(dr["EmployeeID"].ToString());
                objAttendanceInfo.DateAttended = Utility.GetValidDateElseNull(dr["DateAttended"].ToString());
                objAttendanceInfo.StartTime = Utility.GetValidDateElseNull(dr["StartTime"]);
                objAttendanceInfo.EndTime = Utility.GetValidDateElseNull(dr["EndTime"]);
                objAttendanceInfo.DayName = objAttendanceInfo.DateAttended.Value.DayOfWeek.ToString();
                objAttendanceInfo.NoOfHours = Utility.GetNoOfHours(objAttendanceInfo.StartTime, objAttendanceInfo.EndTime);
                clsWorkingHours = new ClsWorkingHours();
                bool found = clsWorkingHours.GetCurrentWorkingHour(objAttendanceInfo.DateAttended.Value, objAttendanceInfo.DateAttended.Value);
                if (found)
                {
                    decimal overTime = objAttendanceInfo.NoOfHours - clsWorkingHours.NoOfHours;
                    if (overTime > 0)
                        objAttendanceInfo.OverTime = overTime;
                    else
                        overTime = 0;
                }
                lstAttendanceInfo.Add(objAttendanceInfo);

            }
            grdAttendance.DataSource = lstAttendanceInfo.OrderBy(d => d.DateAttended).ToList();
            if (lstAttendanceInfo.Count == 0)
            {
                MessageBox.Show("No Data found for the selected date range", "No data found", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtEmpName_ButtonClick()
        {
            DataSet dsLocal = new DataSet();
            Popup popup = new Popup();
            ClsEmployee objEmployee = new ClsEmployee();
            dsLocal.Tables.Add(objEmployee.GetAllActiveEmployees());
            dsLocal.Tables[0].TableName = "popup";
            popup.Width = 50;
            popup.ShowPopup(null, "EmployeeID", ref dsLocal, "popup", "EmployeeID,FirstName,LastName", "", "Select Employee", "ID,FirstName,LastName", "5,20,20");


            if (!string.IsNullOrEmpty(CommonFunction.NullToString(popup.ReturnValue())))
            {
                objEmployee.GetEmployee(Convert.ToInt32(popup.Field("EmployeeID")));
                txtEmpName.Tag = popup.ReturnValue();
                txtEmpName.Text = objEmployee.FullName;
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                ClsAttendance objClsAttendance = new ClsAttendance();
                if (lstAttendanceInfo.Count > 0)
                {
                    objClsAttendance.UpdateEmployeeAttendance(lstAttendanceInfo);
                    MessageBox.Show("Attendance Updated Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                btnSearch_Click(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btnIN_Click(object sender, EventArgs e)
        {
            int employeeID = 0;
            int.TryParse(txtEmpName.Tag.ToString(), out employeeID);
            if (employeeID == 0)
            {
                MessageBox.Show("Plase select employee", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            ClsAttendance obj = new ClsAttendance();
            DateTime date = ddlDate.Value;
            DateTime time = ddlTime.Value;
            DateTime finalDate = new DateTime(date.Year, date.Month, date.Day, time.Hour, time.Month, time.Second);
            try
            {
                obj.SaveEmployeeAttendance("IN", employeeID, finalDate);
                MessageBox.Show("Attendance Updated Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btnOUT_Click(object sender, EventArgs e)
        {
            int employeeID = 0;
            int.TryParse(txtEmpName.Tag.ToString(), out employeeID);
            if (employeeID == 0)
            {
                MessageBox.Show("Plase select employee", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            ClsAttendance obj = new ClsAttendance();
            DateTime date = ddlDate.Value;
            DateTime time = ddlTime.Value;
            DateTime finalDate = new DateTime(date.Year, date.Month, date.Day, time.Hour, time.Month, time.Second);
            try
            {
                obj.SaveEmployeeAttendance("OUT", employeeID, finalDate);
                MessageBox.Show("Attendance Updated Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void ManageAttendance_FormClosing(object sender, FormClosingEventArgs e)
        {
            Control[] Cont = this.ParentForm.Controls.Find("groupBox1", false);
            ((GroupBox)Cont[0]).Visible = true;
            Control[] Cont1 = this.ParentForm.Controls.Find("groupBox2", false);
            ((GroupBox)Cont1[0]).Visible = true;
        }



    }
}
